package com.hentie.spritedrawlib;

import android.content.Context;

import com.hentie.drawlib.IDraw;
import com.hentie.drawlib.OpenGLRenderer;
import com.hentie.drawlib.PlaneMesh;
import com.hentie.drawlib.RenderDraw;
import com.hentie.drawlib.RenderQueue;
import com.hentie.mathlib.Vector3;
import com.hentie.spritelib.SpriteData;
import com.hentie.spritelib.SpriteXMLParser;

public class Sprite implements IDraw {
	//The spritedata
	SpriteData spriteData;
	//The plane representing it
	PlaneMesh planeMesh;

	
    //and Animation based members and methods
    boolean IsFinished;

    //totalFrames and animation
    float frameTimer = 0;
    //This is howmuch we want to increase the frametimer independent of the framerate;
    public float frameDelayFactor;
    //The current frame we are in
    int currentFrame = 0;
    //Current start and End values
    int currentStartFrame = 0;
    int currentEndFrame = 0;
    float currentFrameDelay = 0;
    int currentLoop = -1;
    //The last frame
    int lastFrame = -1;
    //Whether its pause
    boolean paused = false;
    //The current Animation we're in
    String currentAnimation = "IDLE";
    //Whether we're looping
    int loops;
	Vector3 offset = Vector3.Zero();
	
	public Sprite(Context context,String spriteFile)
	{
		//loadthe prite from a file
		spriteData = SpriteXMLParser.ParseXML(context, spriteFile);
		planeMesh = new PlaneMesh(1,1);
		planeMesh.SetTextureName(spriteData.TextureName);
		play(currentAnimation,-1);
	}
	
	public Sprite(Context context,SpriteData data)
	{
		//loadthe prite from a file
		spriteData = data;
		planeMesh = new PlaneMesh(1,1);
		planeMesh.SetTextureName(spriteData.TextureName);
		planeMesh.useTextures = true;
		play(currentAnimation,-1);
	}
	
    //Play animation
    public void play(String Animation, int loop)
    {
        //Setup the animation
        currentAnimation = Animation;
        currentLoop = 0;
        loops = loop;
        //Unpasue it
        paused = false;

        if (spriteData.SpriteAnimation.containsKey(Animation))
        {
            //Setup the frames
            currentStartFrame = (spriteData.SpriteAnimation.get(Animation)).first;
            currentEndFrame = (spriteData.SpriteAnimation.get(Animation)).last;
        }

    }
    
    //Update
    public void Update(float deltaTime, Vector3 position, float angle, Vector3 scale)
    {
        //Frame delay
        currentFrameDelay = spriteData.SpriteFrame.get(currentFrame).frameDelay;
        //If we're not pAUSEs
        if (!paused)
        {
            //
            //Lets makesure the frame isn't outside the current animation bounds
            if (currentFrame > currentEndFrame || currentFrame < currentStartFrame)
                currentFrame = currentStartFrame;

            //Lets see if we've finished looping
            if (loops != -1 && currentLoop > loops)
            {
                IsFinished = true;
                paused = true;
            }

            //Increment the frame
            frameTimer += deltaTime;
            //Do the stuff
            if (frameTimer > currentFrameDelay * frameDelayFactor)
            {
                //If we have lets check if the current frame hasn't reached the end
                if (currentFrame < currentEndFrame)
                {
                    //If it hasnt then increase the Frame
                    currentFrame++;
                }
                else
                {

                    currentLoop++;
                    if (loops == -1 || currentLoop < loops)
                    {
                        //Otherwise start from the beginning
                        currentFrame = currentStartFrame;
                    }
                }
                //Reset the frametimer
                frameTimer = 0;

                //Get the current sprite data
                SpriteData.SpriteFrameInfo data = spriteData.SpriteFrame.get(currentFrame);
                planeMesh.UpdatePlane((data.planeWidth / 2.0f)*scale.X(), (data.planeHeight / 2.0f)*scale.Y(), data.textureX, data.textureY, data.spriteWidth, data.spriteHeight);
                offset.Set(-data.offsetX, data.offsetY, 0);
            }
        }
        position.Subtract(offset);
        //Update the positions
        planeMesh.position = position;
        planeMesh.rotation = angle;
        planeMesh.scale = scale;
        
    }

	public void Draw(RenderQueue queue, float deltaTime) {
		// TODO Auto-generated method stub
		queue.AddToRenderQueue(planeMesh);
		
	}


	

}
